package com.qk.controller;

import com.qk.commen.PageResult;
import com.qk.commen.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/users")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 分页查询用户列表
     */
    @RequestMapping
    public Result list(UserDto userDto) {
        log.info("查询用户列表: {}", userDto);
        PageResult<User> pageResult = userService.list(userDto);
        return Result.success(pageResult);
    }
    /**
     * 删除用户
     * param 必须传入
     * /users/1
     * /users/10,20
     * /users/2,3,4
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除用户: {}", ids);
        userService.delete(ids);
        return Result.success();
    }
    /**
     * 添加用户
     */
    @PostMapping
    public Result add(@RequestBody User user) {
        log.info("添加用户: {}", user);
        userService.add(user);
        return Result.success();
    }
    /**
     * select user by id
     */
    @GetMapping("/{id}")
    public Result getUserById(@PathVariable Integer id) {
        log.info("查询用户: {}", id);
        User user = userService.getUserById(id);
        return Result.success(user);
    }
    /**
     * update user
     * 测试失败,可修改
     */
    @PutMapping
    public Result update(@RequestBody User user) {
        log.info("更新用户: {}", user);
        userService.update(user);
        return Result.success();

    }
    /**
     * select all users
     * 返回数据结构与接口定义不一致
     * 1. $.data 应当是 object,null 类型
     */
    @GetMapping("/list")
    public Result listAll() {
        log.info("查询所有用户信息: {}", userService);
        List<User> list = userService.listAll();
        return Result.success(list);
    }
    /**
     * select users by roles
     */
    @GetMapping("role/{roleLabel}")
    public Result listByRole(@PathVariable String roleLabel) {
        log.info("查询角色为{}的用户信息: {}", roleLabel, userService);
        List<User> list = userService.listByRole(roleLabel);
        return Result.success(list);
    }
    /**
     * select users by dept
     */
    @GetMapping("dept/{deptId}")
    public Result listByDept(@PathVariable Integer deptId) {
        log.info("查询部门为{}的用户信息: {}", deptId, userService);
        List<User> list = userService.listByDept(deptId);
        return Result.success(list);
    }
}
